import UserModel from "../../../models/User";

const userModel = new UserModel()

Page({
  data: {
    username: '',
    password: '',
  },

  bindUsername(e) {
    this.setData({ username: e.detail.value.trim() })
  },

  bindPassword(e) {
    this.setData({ password: e.detail.value.trim()})
  },

  onSave() {
    if (!this.data.username) {
      return wx.showToast({title: '请输入用户名', icon: 'error', mask: true})
    }
    if (!this.data.password) {
      return wx.showToast({title: '请输密码', icon: 'error', mask: true})
    }

    userModel.merchantLogin({ ...this.data }).then(response => {

      const { data } = response
      wx.setStorageSync('merchant_id', data.id)

      wx.showToast({title: '登录成功', icon: 'none', mask: true})

      const page = wx.getStorageSync('merchant-page')

      setTimeout(() => {
        wx.navigateTo({
          url: page ? page : '/sub_main/pages/merchant/index'
        })
      }, 500)
    })
  },

  onCenter() {
    wx.switchTab({
      url: '/pages/center/index'
    })
  },

  onUnload() {
    wx.switchTab({
      url: '/pages/center/index'
    })
  }

});
